package fcc.Seng462project.server;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;

//import com.google.appengine.api.datastore.Query;
import javax.jdo.Query;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;


import javax.mail.MessagingException;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


import fcc.Seng462project.client.UserLogIn;



public class UserLogInImpl extends RemoteServiceServlet implements UserLogIn {
	private static final long serialVersionUID = 1l;
	private static final Logger LOG=Logger.getLogger(UserLogInImpl.class.getName());
	
	private static final PersistenceManagerFactory PMF=JDOHelper.getPersistenceManagerFactory("transactions-optional");
	//after server has recieve user information
   //it will store UserDB entity to system
   //after succeed, it will send a notification to user by email
	public void registerUser(String data){
		String[] uInfo;
		uInfo=data.split(",");
		PersistenceManager pm = getPersistenceManager();
		try{
			String emailAddress=uInfo[0];
			String password=uInfo[1];
			pm.makePersistent(new UserDB(emailAddress,password));
			sendEmail(emailAddress,"Welcome to FileCrate","You have succeed register FileCrate account");
		}finally{
			pm.close();
		}
		
	}

	/*check if user already exist, if yes, send notication to client side*/
	public String loginUser(String data){
		String[] uInfo;
		uInfo=data.split(",");
		PersistenceManager pm=getPersistenceManager();
		List<String> symbols=new ArrayList<String>();
		//Query query =pm.newQuery(UserDB.class);
		//UserDB user=new UserDB();
		//Object idInstance=getPersistenceManager().newObjectIdInstance(UserDB.class, user.getEmailAddress());
		
		//UserDB user=pm.getObjectById(UserDB.class,uInfo[0]);
		String ret=null;
		try{
			UserDB user=pm.getObjectById(UserDB.class,uInfo[0]);
			if(user.getEmailAddress().equals(uInfo[0])&&user.getPassword().equals(uInfo[1])){
				ret=uInfo[0];
			}else{
				ret="Error";
			}
			
			/*
			String query="select from"+UserDB.class.getName();
			List<UserDB> results=(List<UserDB>)query.execute(uInfo[0]);
			Window.alert(uInfo[0]);
			if(!results.isEmpty()){
				for(UserDB e:results){
					if(e.getEmailAddress().equals(uInfo)&&e.getPassword().equals(uInfo[1])){
						ret=e.getEmailAddress();
					}else{
						ret="Error";
					}
				}
			
			}
		*/
		}finally{
			pm.close();
		}
		return ret;
	}
	
	private void sendEmail(String email, String title, String body) {
		// TODO Auto-generated method stub
		Properties props = new Properties();
		Session session =Session.getDefaultInstance(props,null);
		try{
			Message msg= new MimeMessage(session);
			msg.setFrom(new InternetAddress("zhaiyaning@gmail.com"));
			msg.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
			msg.setSubject(title);
			msg.setText(body);
			Transport.send(msg);
		}catch(AddressException e){
			e.printStackTrace();
		}catch(MessagingException e){
			e.printStackTrace();
		}
		
	}
	
	private PersistenceManager getPersistenceManager() {
		return PMF.getPersistenceManager();
	}

}
